package testing;

import clang.control.CodeBlock;
import clang.control.ExecutionThread;
import clang.control.ReturnStatement;
import clang.control.VariableDefinition;
import clang.control.WhileLoop;
import clang.data.Expression;
import clang.data.VarName;
import clang.data.VarType;
import clang.data.Variable;
import clang.functions.Function;
import clang.functions.FunctionCall;
import clang.ops.Operator;

public class LoopTest {
	public static void main(String[] args) {
		
		CodeBlock cb = new CodeBlock(ExecutionThread.get().getCurrentBlock());
		
		cb.append(new VariableDefinition("i", VarType.INT));		// int i;
		cb.append(new Operator("=", new VarName("i"), new Variable(VarType.INT, 0.0)));	// i = 5;
		
		Expression cond = new Operator("<", new VarName("i"), new Variable(VarType.INT, 5));	// i < 5
		
		CodeBlock wblock = new CodeBlock(cb);
		
		FunctionCall fc = new FunctionCall("dbgprint");	// dbgprint(i);
		fc.addParameter(new VarName("i"));
		wblock.append(fc);
		
		wblock.append(new Operator("x++", new VarName("i")));	// i++;
		
		cb.append(new WhileLoop(wblock, cond));	// while(i < 5) { dbgprint(i); i++; }
		cb.append(new ReturnStatement(0));	// return 0;
		
		Function mainFunc = new Function("main"); // int main() { ... }
		mainFunc.setReturnType(VarType.INT);
		mainFunc.setBlock(cb);
		ExecutionThread.get().addFunction(mainFunc);
		
		try {
			ExecutionThread.get().runProgram();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
